<div class="main-content">
    <div class="section__content section__content--p20">
        <div class="container-fluid">
            <div class="row m-b-30">
                <div class="col-lg-12">
                    <div class="overview-wrap">
                        <h2 class="title-1"><?php echo $id == 0 ? "新建图片" : "编辑图片" ?></h2>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                            <strong><?php echo $id == 0 ? "新建图片" : "编辑图片" ?></strong>
                        </div>
                        <div class="card-body">
                            <form class="form-horizontal" role="form" name="form" id="form" method="post"
                                  data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
                                  data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
                                  data-bv-feedbackicons-validating="glyphicon glyphicon-refresh"
                                  action="index.php?r=admin/image/edit">
                                <input type="hidden" name="id" value="<?php echo $id ?>"/>
                                <input type="hidden" name="YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>"/>
                                <input type="hidden" name="cansale" value="0">

                                <div class="form-group">
                                    <label class=" form-control-label">标题</label>
                                    <input type="text" name="title" class=" form-control "
                                           value="<?php echo $image == null ? "" : $image['title'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">模块</label>

                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label"></label>
                                    <select class="form-control" name="moduleid">
                                        <?php
                                        if (sizeof($modules) > 0) {
                                            foreach ($modules as $m) {
                                                if ($image['module_id'] == $m['module_id']) {
                                                    ?>
                                                    <option value="<?php echo $m['module_id']; ?>"
                                                            selected="selected"><?php echo $m['category']; ?></option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option
                                                            value="<?php echo $m['module_id']; ?>"><?php echo $m['category']; ?></option>
                                                    <?php
                                                }
                                            }
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">内容</label>
                                    <textarea name="description" class="form-control"
                                              style="width: 100%;height: 300px;"><?php echo $image == null ? "" : $image['description'] ?></textarea>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">图片</label>
                                    <input type="text" name="image" id="image" class="form-control "
                                           value="<?php echo $image == null ? "" : $image['image'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label"></label>
                                    <input type="button" value="选择图片" class="btn btn-primary btn-sm " id="selectImage">
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">排序</label>
                                    <input type="text" name="seq" class="form-control "
                                           value="<?php echo $image == null ? "" : $image['seq'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">标签</label>
                                    <input type="text" name="tags" class="form-control "
                                           value="<?php echo $image == null ? "" : $image['tags'] ?>"/>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">价格</label>
                                    <?php
                                    if ($image == null) {
                                        ?>
                                        <input type="text" name="price" class="form-control " value="0"/>
                                        <?php
                                    } else {
                                        ?>
                                        <input type="text" name="price" class="form-control "
                                               value="<?php echo $image['price']; ?>"/>
                                        <?php
                                    }
                                    ?>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">推荐</label>
                                    <select name="recommend" class="form-control">
                                        <?php
                                        if ($image != null && $image['recommend'] == 1) {
                                            ?>
                                            <option value="0">否</option>
                                            <option value="1" selected="selected">是</option>
                                            <?php
                                        } else {
                                            ?>
                                            <option value="0">否</option>
                                            <option value="1">是</option>
                                            <?php
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label class=" form-control-label">置顶</label>
                                    <select name="top" class="form-control">
                                        <?php
                                        if ($image != null && $image['top'] == 1) {
                                            ?>
                                            <option value="0">否</option>
                                            <option value="1" selected="selected">是</option>
                                            <?php
                                        } else {
                                            ?>
                                            <option value="0">否</option>
                                            <option value="1">是</option>
                                            <?php
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <button  type="submit" name="submit" id="submit" class="btn btn-primary btn-lg btn-block">保存</button>
                                </div>

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="<?php echo Yii::app()->request->baseUrl ?>/static/admin/lib/kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="<?php echo Yii::app()->request->baseUrl ?>/static/admin/lib/kindeditor/lang/zh_CN.js"></script>
<script>
    var editor = null;
    $(function(){
        initPage();
    });
    function initPage() {
        editor = initEdit("description",'image')

        KindEditor.ready(function(K) {
            var editor = K.editor({
                fileManagerJson:'static/admin/lib/kindeditor/file_manager_json.php?module=image',
            });
            K('#selectImage').click(function() {
                editor.loadPlugin('filemanager', function() {
                    editor.plugin.filemanagerDialog({
                        viewType : 'VIEW',
                        dirName : 'image',
                        clickFn : function(url, title) {
                            // K('#url').val(url);
                            $("input[name=image]").val(url);
                            editor.hideDialog();
                        }
                    });
                });
            });
        });

        $('#form').bootstrapValidator();
        $('#form').ajaxForm({
            dataType: 'json',
            beforeSubmit: function (formData, jqForm, options) {
                return $('#form').data('bootstrapValidator').isValid();
            },
            success: function (res) {
                if (message(res)) {
                    window.location.href = "index.php?r=admin/content/index&type=image";
                }
            }
        });
    }
</script>